/**
 * 报考学校管理初始化
 */
var EnrollSchool = {
    id: "EnrollSchoolTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
EnrollSchool.initColumn = function () {
    return [
            {title: '主键ID', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '报考学校代号', field: 'schoolCode', visible: true, align: 'center', valign: 'middle'},
            {title: '报考学校名称', field: 'schoolName', visible: true, align: 'center', valign: 'middle'},
            {title: '参与报考批次', field: 'batchTypeNames', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
EnrollSchool.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        EnrollSchool.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加报考学校
 */
EnrollSchool.openAddEnrollSchool = function () {
    var index = layer.open({
        type: 2,
        title: '添加报考学校',
        area: ['768px', ' 500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/enrollSchool/enrollSchool_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看报考学校详情
 */
EnrollSchool.openEnrollSchoolDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '报考学校详情',
            area: ['768px', '500px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/enrollSchool/enrollSchool_update/' + EnrollSchool.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除报考学校
 */
EnrollSchool.delete = function () {
    if (this.check()) {
        Feng.confirm("确定删除该报考学校？",function(){
            var ajax = new $ax(Feng.ctxPath + "/enrollSchool/delete", function (data) {
                Feng.success("删除成功!");
                EnrollSchool.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("enrollSchoolId",EnrollSchool.seItem.id);
            ajax.start();
        });
    }
};


EnrollSchool.resetSearch = function () {
    $("#condition").val("");
    EnrollSchool.search();
}

/**
 * 查询报考学校列表
 */
EnrollSchool.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    EnrollSchool.table.refresh({query: queryData});
};

/**
 * 配置专业
 */
EnrollSchool.configBatchMajor=function(){
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '配置报考专业',
            area: ['1100px', '700px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/enrollSchoolBatchMajor/' + EnrollSchool.seItem.id
        });
        this.layerIndex = index;
    }
};

EnrollSchool.templateDown = function () {
    var operation = function(){
        location.href=Feng.ctxPath + "/static/file/报考学校导入模板.xlsx";
    };
    Feng.confirm("确定下载报考学校导入模板？",operation);
};

EnrollSchool.templateDown1 = function () {
    var operation = function(){
        location.href=Feng.ctxPath + "/static/file/报考专业导入模板.xlsx";
    };
    Feng.confirm("确定下载报考专业导入模板？",operation);
};

/*
 * 报考学校导入
 */
EnrollSchool.importEnrollSchool = function () {
	 //定义上传文件对象
   var webUploaderObj = new $WebUpload("importEnrollSchool");
   //设置允许上传的文件类型（在web-upload-object中定义了类型）
   webUploaderObj.setAcceptFileType("excel");
   //设置文件上传地址
   webUploaderObj.setUploadUrl(Feng.ctxPath + "/enrollSchool/importEnrollSchool");
   //获取webUploader对象，调用create方法，手动绑定事件
   var webUploader = webUploaderObj.create();
   //绑定事件
   webUploader.on('uploadSuccess', function (file, response) {
       Feng.infoDetail("导入结果", response.message.split("\n"), '500px', '500px');
       EnrollSchool.table.refresh();
   });
   //文件上传失败，显示上传出错。
   webUploader.on('uploadError', function (file, response) {
       Feng.error("上传失败");
   });
};

/*
 * 报考专业导入
 */
EnrollSchool.importMajorEnrollSchool = function () {
	 //定义上传文件对象
   var webUploaderObj = new $WebUpload("majorimportEnrollSchool");
   //设置允许上传的文件类型（在web-upload-object中定义了类型）
   webUploaderObj.setAcceptFileType("excel");
   //设置文件上传地址
   webUploaderObj.setUploadUrl(Feng.ctxPath + "/enrollSchool/importMajor");
   //获取webUploader对象，调用create方法，手动绑定事件
   var webUploader = webUploaderObj.create();
   //绑定事件
   webUploader.on('uploadSuccess', function (file, response) {
       Feng.infoDetail("导入结果", response.message.split("\n"), '500px', '500px');
       EnrollSchool.table.refresh();
   });
   //文件上传失败，显示上传出错。
   webUploader.on('uploadError', function (file, response) {
       Feng.error("上传失败");
   });
};

$(function () {
    var defaultColumns = EnrollSchool.initColumn();
    var table = new BSTable(EnrollSchool.id, "/enrollSchool/list", defaultColumns);
    EnrollSchool.table = table.init();
    EnrollSchool.importEnrollSchool();
    EnrollSchool.importMajorEnrollSchool();
});
